﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M014_MerberBuy : Form
    {
        private const string dataName = "会员消费记录";

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="MemberID"></param>
        /// <param name="MemberNO"></param>
        /// <param name="MemberName"></param>
        public M014_MerberBuy (string MemberID,string MemberNO,string MemberName)
        {
            InitializeComponent ();
            lbMemberID.Text = MemberID;
            lbUSER_NO.Text = MemberNO;
            lbUSER_NAME.Text = MemberName;
        }

        #region 事件

        private void btnClose_Click ( object sender, EventArgs e )
        {
            this.Close ();
        }

        /// <summary>
        /// 添加消费记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click ( object sender, EventArgs e )
        {
            Cursor.Current = Cursors.WaitCursor;
            if (!CheckProcessLoop (enumOperation.Add))
            {
                this.Cursor = Cursors.Default;
                return;
            }
            MODEL.MOD_M014_MerberBuy model = new XiaoGang.Soft.XGWMS.MODEL.MOD_M014_MerberBuy ();
            model.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID.Trim ();
            model.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD.Trim ();
            model.USER_ID = lbMemberID.Text;
            model.GOODS_NAME = txtGOODS_NM.Text.Trim ();
            model.PRICE = txtPrice.Text.Trim () == "" ? -1 : decimal.Parse (txtPrice.Text.Trim ());
            model.NUITS = txtNUITS.Text.Trim () == "" ? -1 : int.Parse (txtNUITS.Text.Trim ());
            model.TOTAL = txtTOTAL.Text.Trim () == "" ? -1 : decimal.Parse (txtTOTAL.Text.Trim ());
            model.DATETEME = DateTime.Parse (txtDATETEME.Text.Trim ());
            XiaoGang.Soft.XGWMS.BLL.BLL_M014_MerberBuy bll = new XiaoGang.Soft.XGWMS.BLL.BLL_M014_MerberBuy ();
            ReturnValue reValue = bll.Add (model);
            if (reValue.IsSuccess)
            {
                ShowData ();
                Cursor.Current = Cursors.Default;
                return;
            }
            else
            {
                Method.MessageShow(reValue.ErrorID, dataName);
                Cursor.Current = Cursors.Default;
                return;
            }
        }

        private void dateTimePickerUSER_BIRTHDAY_CloseUp ( object sender, EventArgs e )
        {
            txtDATETEME.Text = Method.TimeFormat (dtpBUSINESS_TIME.Value.ToString ("yyyyMMdd"));
        }

        private void txtPrice_TextChanged ( object sender, EventArgs e )
        {
            if (txtPrice.Text.Trim ().Length < 1 || txtNUITS.Text.Trim ().Length < 1)
            {
                txtTOTAL.Text = "";
            }
            else
            {
                try
                {
                    txtTOTAL.Text = string.Format ("{0:f2}", decimal.Parse (txtNUITS.Text) * decimal.Parse (txtPrice.Text));
                }
                catch
                {

                }
            }
        }

        private void M014_MerberBuy_KeyUp ( object sender, KeyEventArgs e )
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    btnAdd_Click (sender, e);
                    break;
                case Keys.Escape:
                    btnClose_Click (sender, e);
                    break;
            }
        }

        private void M014_MerberBuy_Load ( object sender, EventArgs e )
        {
            ClearControl ();
            SetTabIndex ();
            InitialGridView ();
            ShowData ();
        }

        private void grdMain_CellClick ( object sender, DataGridViewCellEventArgs e )
        {
            int intColumnIndex = e.ColumnIndex;
            if (intColumnIndex == 7 && e.RowIndex != -1)
            {
                if (grdMain.CurrentRow.Cells["ID"].Value.ToString ().Trim ().Length < 1)
                {
                    Method.MessageShow ("W11052","");//正常业务数据不能删除！
                    return;
                }
                if (Method.MessageShow ("Q22007","") == DialogResult.Yes)
                {                    
                    XiaoGang.Soft.XGWMS.BLL.BLL_M014_MerberBuy bll = new XiaoGang.Soft.XGWMS.BLL.BLL_M014_MerberBuy ();
                    ReturnValue rValue = bll.Delete(grdMain.CurrentRow.Cells["ID"].Value.ToString ().Trim ());
                    if (rValue.IsSuccess)
                    {
                        ShowData ();
                    }
                    else
                    {
                        Method.MessageShow(rValue.ErrorID, dataName);
                        txtTOTAL.Focus ();
                    }
                }
            }
        }

        #endregion

        #region 方法

        private void ShowData()
        {
            XiaoGang.Soft.XGWMS.BLL.BLL_M014_MerberBuy bll = new XiaoGang.Soft.XGWMS.BLL.BLL_M014_MerberBuy ();
            ReturnValue reValue = bll.GetList(lbMemberID.Text);
            if (reValue.IsSuccess)
            {
                grdMain.Rows.Clear ();
                for (int i = 0; i < reValue.ResultDataSet.Tables[0].Rows.Count; i++)
                {
                    grdMain.Rows.Insert (grdMain.Rows.Count,
                        (i + 1).ToString (),
                        reValue.ResultDataSet.Tables[0].Rows[i]["ID"].ToString ().Trim (),
                        reValue.ResultDataSet.Tables[0].Rows[i]["BUSINESS_TIME"].ToString ().Trim (),
                        reValue.ResultDataSet.Tables[0].Rows[i]["GOODS_NM1"].ToString ().Trim (),
                        reValue.ResultDataSet.Tables[0].Rows[i]["SELLING_PRICE"].ToString ().Trim ()=="" ? "":string.Format("{0:f2}",decimal.Parse(reValue.ResultDataSet.Tables[0].Rows[i]["SELLING_PRICE"].ToString ().Trim ())),
                        reValue.ResultDataSet.Tables[0].Rows[i]["UNITS"].ToString ().Trim () == "" ? "" : string.Format ("{0:f2}", decimal.Parse (reValue.ResultDataSet.Tables[0].Rows[i]["UNITS"].ToString ().Trim ())),
                        reValue.ResultDataSet.Tables[0].Rows[i]["AllPrice"].ToString ().Trim () == "" ? "" : string.Format ("{0:f2}", decimal.Parse (reValue.ResultDataSet.Tables[0].Rows[i]["AllPrice"].ToString ().Trim ())),
                        "删除");
                }
                SetGridBackColor ();
            }
            else
            {
                Method.MessageShow(reValue.ErrorID,dataName);
            }
            Cursor.Current = Cursors.Default;
        }
        
        private void SetTabIndex ( )
        {
            int intIndex = 0;
            dtpBUSINESS_TIME.TabIndex = intIndex; intIndex++;
            txtGOODS_NM.TabIndex = intIndex; intIndex++;
            txtPrice.TabIndex = intIndex; intIndex++;
            txtNUITS.TabIndex = intIndex; intIndex++;
            txtTOTAL.TabIndex = intIndex; intIndex++;
            btnAdd.TabIndex = intIndex; intIndex++;
            btnClose.TabIndex = intIndex; intIndex++;
        }

        private void ClearControl ( )
        {
            txtDATETEME.Text = DateTime.Now.ToString ("yyyyMMdd").Insert (4, "/").Insert (7, "/");
            txtGOODS_NM.Text = "";
            txtPrice.Text = "";
            txtNUITS.Text = "";
            txtTOTAL.Text = "";
        }        

        protected void InitialGridView ( )
        {
            grdMain.AllowUserToAddRows = false;
            grdMain.AllowUserToResizeColumns = true;
            grdMain.AllowUserToDeleteRows = false;
            grdMain.AllowUserToResizeRows = false;
            grdMain.ColumnHeadersHeight = 30;
            grdMain.ColumnHeadersVisible = true;
            grdMain.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdMain.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdMain.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdMain.ReadOnly = true;
            grdMain.RowHeadersVisible = false;
            grdMain.RowTemplate.Resizable = DataGridViewTriState.False;
            grdMain.RowTemplate.Height = 25;
            grdMain.RowTemplate.ReadOnly = true;
            grdMain.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdMain.ScrollBars = ScrollBars.Both;
            grdMain.TabStop = false;

            grdMain.Columns.Add ("NO.", "NO.");
            grdMain.Columns.Add ("ID", "");
            grdMain.Columns.Add ("DATETEME", "时间");
            grdMain.Columns.Add ("GOODS_NAME", "商品名称");
            grdMain.Columns.Add ("PRICE", "单价");
            grdMain.Columns.Add ("NUITS", "数量");
            grdMain.Columns.Add ("TOTAL", "合计");
            DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn ();
            btnDelete.Text = "删除";
            btnDelete.Width = 80;
            btnDelete.HeaderText = "删除";
            grdMain.Columns.Add (btnDelete);

            grdMain.Columns["NO."].Width = 50;
            grdMain.Columns["NO."].Frozen = true;
            grdMain.Columns["NO."].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdMain.Columns["NO."].Resizable = DataGridViewTriState.False;
            grdMain.Columns["NO."].SortMode = DataGridViewColumnSortMode.NotSortable;

            grdMain.Columns["ID"].Visible = false;
            grdMain.Columns["DATETEME"].Width = 130;
            grdMain.Columns["DATETEME"].Frozen = true;
            grdMain.Columns["DATETEME"].Resizable = DataGridViewTriState.False;
            grdMain.Columns["DATETEME"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["DATETEME"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            grdMain.Columns["GOODS_NAME"].Width = 150;
            grdMain.Columns["GOODS_NAME"].Frozen = true;
            grdMain.Columns["GOODS_NAME"].Resizable = DataGridViewTriState.False;
            grdMain.Columns["GOODS_NAME"].SortMode = DataGridViewColumnSortMode.NotSortable;

            grdMain.Columns["PRICE"].Width = 120;
            grdMain.Columns["PRICE"].Frozen = true;
            grdMain.Columns["PRICE"].Resizable = DataGridViewTriState.False;
            grdMain.Columns["PRICE"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["PRICE"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            grdMain.Columns["NUITS"].Width = 100;
            grdMain.Columns["NUITS"].Resizable = DataGridViewTriState.False;
            grdMain.Columns["NUITS"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["NUITS"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            grdMain.Columns["TOTAL"].Width = 200;
            grdMain.Columns["TOTAL"].Resizable = DataGridViewTriState.False;
            grdMain.Columns["TOTAL"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdMain.Columns["TOTAL"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            btnDelete.Resizable = DataGridViewTriState.False;
        }        

        private void SetGridBackColor ( )
        {
            for (int i = 0; i < grdMain.Rows.Count; i++)
            {
                if (grdMain.Rows[i].Cells["ID"].Value.ToString ().Trim () .Length<1)
                {
                    grdMain.Rows[i].DefaultCellStyle.BackColor = Color.PaleTurquoise; 
                }
            }
        }

        /// <summary>
        /// <p><b>方法名称</b>CheckProcess</p>	
        /// <p><b>概要説明  :</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日	:</b>2010/08/20</p>	
        /// <p><b>修改历史:</b></p>	
        /// </summary>	
        /// <param name="strConstrolName">控件名称</param>
        /// <returns></returns>
        protected bool CheckProcess ( string strConstrolName )
        {
            string strValue = string.Empty;
            try
            {
                switch (strConstrolName)
                {
                    case "txtGOODS_NM"://商品名称
                        strValue = this.txtGOODS_NM.Text.Trim ();                        
                        if (Encoding.Default.GetByteCount (strValue) > txtGOODS_NM.MaxLength)
                        {
                            Method.MessageShow ("W11007","商品名称");//
                            txtGOODS_NM.Focus ();
                            return false;
                        }
                        break;
                    case "txtPrice"://价格
                        strValue = this.txtPrice.Text.Trim ().Replace (",", "");
                        if (!Method.CheckIsNumerc (strValue, txtPrice.MaxLength, true, true))
                        {
                            Method.MessageShow(GlobalVal.glostrMsg,"价格");
                            txtPrice.Focus ();
                            return false;
                        }
                        break;
                    case "txtNUITS"://数量
                        strValue = this.txtNUITS.Text.Trim ().Replace (",", "");
                        if (!Method.CheckIsNumber (strValue, txtNUITS.MaxLength, true, true))
                        {
                            Method.MessageShow(GlobalVal.glostrMsg,"数量");
                            txtNUITS.Focus ();
                            return false;
                        }
                        break;
                    case "txtTOTAL"://合计
                        strValue = this.txtTOTAL.Text.Trim ().Replace (",", "");
                        if (!Method.CheckIsNumerc (strValue, txtTOTAL.MaxLength, false, true))
                        {
                            Method.MessageShow(GlobalVal.glostrMsg,"合计");
                            txtTOTAL.Focus ();
                            return false;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteClientErrLog4Net (ex);
                return false;
            }
            return true;
        }

        /// <summary>
        /// <p><b>方法名：</b>CheckProcessLoop</p>	
        /// <p><b>概要説明:</b>画面のデータのＣＨＥＣＫ</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2010/08/20</p>	
        /// <p><b>修改历史:</b></p>	
        /// </summary>	
        /// <param name="operation">Check状态枚举</param>
        /// <returns></returns>
        protected bool CheckProcessLoop ( enumOperation emOperation )
        {
            switch (emOperation)
            {
                case enumOperation.Add:
                    if (!CheckProcess ("txtGOODS_NM")) return false;
                    if (!CheckProcess ("txtPrice")) return false;
                    if (!CheckProcess ("txtNUITS")) return false;
                    if (!CheckProcess ("txtTOTAL")) return false;
                    break;                
                default:
                    break;
            }
            return true;
        }

        #endregion

        /// <summary>
        /// 选择商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectGoods_Click(object sender, EventArgs e)
        {
            if (Cursor.Current == Cursors.WaitCursor)
            {
                return;
            }
            if (GlobalVal.gloSearchGoodsForm == null)
            {
                GlobalVal.gloSearchGoodsForm = new M007_GOODS(true);
            }
            GlobalVal.gloSearchGoodsForm.ShowSearchGoodsDialog(this,"","", txtGOODS_NM.Text);
            if (GlobalVal.gloSelectedGoods.ItemCount > 0)
            {
                txtGOODS_NM.Text=GlobalVal.gloSelectedGoods.SelectedItems[0].GOODS_NM1;
                txtPrice.Text = GlobalVal.gloSelectedGoods.SelectedItems[0].SalePrice.ToString();
            }
        }
    }
}